package com.init.engineering.service.impl;

import com.init.engineering.dao.ContractorDao;
import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Contractor;
import com.init.engineering.service.ContractorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ContractorServiceImpl implements ContractorService {
    @Autowired(required = false)
    private ContractorDao contractorDao;

    /**
     * 找到所有承包商信息
     * @return
     */
    @Override
    public List<Contractor> findContractors() {
        List<Contractor> contractors = contractorDao.findContractors();
        return contractors;
    }

    /**
     * 分页查询所有承包商
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findContractorsByPage(QueryPageBean queryPageBean) {
        Integer begin = (queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize();
        queryPageBean.setCurrentPage(begin);
        //查询当前页的数据集合
        List<Contractor> contractors = contractorDao.findContractorsByPage(queryPageBean);
        //获取分页查询的条件
        String queryString = queryPageBean.getQueryString();
        //获取分页查询按条件查询的总条数
        long totalCount = contractorDao.selectTotalCount(queryString);

        //将数据封装到PageResult内
        PageResult pageResult = new PageResult();
        pageResult.setTotal(totalCount);
        pageResult.setRows(contractors);
        return pageResult;
    }

    /**
     * 删除一个承包商
     * @param cid
     * @return
     */
    @Override
    public int deleteContractor(String cid) {
        int row = contractorDao.updateStatusContractor(cid);
        return row;
    }

    /**
     * 通过Cid找打承包商
     * @param cid
     * @return
     */
    @Override
    public Contractor findContractorByCid(String cid) {
        Contractor contractor = contractorDao.findConByCid(cid);
        return contractor;
    }

    /**
     * 新增承包商
     * @param contractor
     * @return
     */
    @Override
    public int saveContractor(Contractor contractor) {
        int rows = contractorDao.saveContractor(contractor);
        return rows;
    }


}
